package com.tcm.item.service.drugs;

import cn.hutool.core.util.ObjectUtil;
import com.base.core.utils.R;
import com.base.genid.generator.UidGenerator;
import com.base.core.dozer.DozerConvert;
import com.base.core.utils.ListUtils;
import com.tcm.item.domain.dto.DosageFormDTO;
import com.tcm.item.domain.entity.DosageForm;
import com.tcm.item.domain.enums.BusinessCodeEnum;
import com.tcm.item.mapper.DosageFormMapper;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.List;


@DubboService(version = "0.0.1-alpha", retries = 3, timeout = 3000)
public class DosageFormServiceImpl implements DosageFormService {

    @Autowired
    private DosageFormMapper dosageFormMapper;

    @DubboReference(version = "0.0.1-beta", group = "cached-uid", retries = 3, timeout = 3000, check = false)
    private UidGenerator uidGenerator;


    @Override
    public int save(DosageForm dosageForm) {

        List<Long> ids = dosageFormMapper.checkNameExists(dosageForm.getName(), null);
        if (ListUtils.isNotEmpty(ids)) {
            R.throwFail(BusinessCodeEnum.PRODUCT_DOSAGE_FORM_CAN_NOT_DUPLICATE);
        }

        dosageForm.setId(uidGenerator.getUID());
        dosageForm.setCreateTime(LocalDateTime.now());
        dosageForm.setUpdateTime(LocalDateTime.now());
        dosageForm.setStatus(new Byte("1"));
        dosageForm.setDeleted(new Byte("0"));
        return dosageFormMapper.insert(dosageForm);

    }

    @Override
    public int update(DosageForm dosageForm) {
        List<Long> ids = dosageFormMapper.checkNameExists(dosageForm.getName(), dosageForm.getId());
        if (ListUtils.isNotEmpty(ids)) {
            R.throwFail(BusinessCodeEnum.PRODUCT_DOSAGE_FORM_CAN_NOT_DUPLICATE);
        }
        dosageForm.setUpdateTime(LocalDateTime.now());
        return dosageFormMapper.update(dosageForm);
    }

    @Override
    public DosageFormDTO findById(Long id) {
        DosageForm dosageForm = dosageFormMapper.findById(id);
        if(ObjectUtil.isEmpty(dosageForm)){
            return null;
        }
        DosageFormDTO dosageFormDTO = DozerConvert.copyProperties(dosageForm, DosageFormDTO.class);
        return dosageFormDTO;
    }

    @Override
    public int deleteById(Long id) {
        return dosageFormMapper.deleteById(id);
    }

    @Override
    public int deleteLogicById(Long id) {
        return dosageFormMapper.deleteLogicById(id);
    }

    @Override
    public List<DosageFormDTO> findDosageFormList() {
        List<DosageForm> dosageFormList = dosageFormMapper.findDosageFormList();
        if(ObjectUtil.isEmpty(dosageFormList)){
            return null;
        }
        return DozerConvert.copyList(dosageFormList, DosageFormDTO.class);
    }
}
